package com.xh.bussiness.permission.intercept;

import com.xh.bussiness.permission.service.IUserRoleService;
import com.xh.bussiness.permission.service.IUserService;
import com.xh.core.properties.XhProperties;
import com.xh.core.util.CacheUtils;
import com.xh.core.util.StringUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.lang.Nullable;
import org.springframework.stereotype.Component;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.handler.HandlerInterceptorAdapter;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Map;

/*
 * @Author daiwei
 * @Description
 * @Date 2018/8/16 08:37
 **/
@Slf4j
@Component
public class PermisstionInterceptor extends HandlerInterceptorAdapter {

    @Autowired
    private IUserService userService;
    @Autowired
    private IUserRoleService userRoleService;
    @Autowired
    private XhProperties xhProperties;

    private String userFunctions="";

    private boolean isAnsynJsonRequest(HttpServletRequest request){
        System.out.println(request.getHeader("x-requested-with"));
        if (request.getHeader("x-requested-with") != null && request.getHeader("x-requested-with").equalsIgnoreCase("XMLHttpRequest")) { //如果是ajax请求响应头会有x-requested-with
            return true;
        }
        return false;
    }

    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        String url=request.getServletPath();
        String userId="";
        userFunctions="";

        Cookie[] cookies= request.getCookies();
        if(cookies!=null&&cookies.length>0){
            for (Cookie c: cookies) {
                if("userId".equals(c.getName())){
                    userId=c.getValue();
                }
            }
        }
        if(StringUtils.isEmpty(userId)){
            //response.setStatus(403);
            //response.setHeader("XH_STATUS", "noPermission");
            //return false;
        }else{
            Map<String,Map<String,Object>> permisstions=(Map<String,Map<String,Object>>) CacheUtils.get("permissions_"+userId);
            if(permisstions!=null) {
                if (permisstions.containsKey(url)) {
                    userFunctions=(String)permisstions.get(url).get("user_functions");
                } else {
                    //response.setStatus(403);
                    //response.setHeader("XH_STATUS", "noPermission");
                    //return false;
                }
            }else{
                //response.setStatus(403);
                //response.setHeader("XH_STATUS", "noPermission");
                //return false;
            }
        }

        /**
         response.setStatus(403);
         response.setHeader("XH_STATUS", "noPermission");
         return false;
         */

        return super.preHandle(request, response, handler);
    }

    @Override
    public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, @Nullable ModelAndView modelAndView) throws Exception {
        if(modelAndView!=null){
            modelAndView.addObject("urlFunctions", userFunctions);
        }
        super.postHandle(request, response, handler, modelAndView);
    }

    @Override
    public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object object, Exception exception) throws Exception{
        super.afterCompletion(request,response,object,exception);
    }
}